package com.ytplayer.assets 
{
	import com.j4w.Helper;
	import com.j4w.SimpleMask;
	import com.ytplayer.player.YoutubeVideoPlayer;
	import com.ytplayer.remote.Video;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	/**
	 * ...
	 * @author Jakub Wagner, J4W
	 */
	public class SongItem extends Sprite 
	{
		public var tfTrackName:ClickableText;
		public var tfTime:TextField;
		
		public var bg:Sprite;
		
		private var _video:Video;
		
		public function SongItem() 
		{
			tfTrackName.mask = new SimpleMask(tfTrackName.width, tfTrackName.height, 0, 0, this);
			
			tfTrackName.onClickCallback = onTrackNameClick;
		}
		
		/**
		 * Play the video from the Video object in YoutubeVideoPlayer singleton.
		 */
		private function playThisItem():void 
		{
			YoutubeVideoPlayer.getInstance().loadVideoById(_video.url);			
		}
				
		override public function get width():Number 
		{
			return super.width;
		}
		
		override public function set width(value:Number):void 
		{
			bg.width = value;
			tfTime.x = value - tfTime.width;
			tfTrackName.mask.width = value - tfTime.width;
		}
		
		/**
		 * Gets/sets the Video object. Refreshes all data.
		 */
		public function get video():Video 
		{
			return _video;
		}
		
		public function set video(value:Video):void 
		{
			_video = value;
			
			tfTime.text = Helper.timeToString(value.duration);
			tfTrackName.text = value.name;
		}
		
		/**
		 * After track name click.
		 */
		private function onTrackNameClick():void 
		{			
			playThisItem();
		}
		
		
	}

}